package com.athuahua.spzx.service.impl;

import com.athuahua.spzx.entity.SysRoleMenu;
import com.athuahua.spzx.mapper.SysRoleMenuMapper;
import com.athuahua.spzx.deprecated_entity.system.SysMenuHasChildren;
import com.athuahua.spzx.vo.system.SysMenuVo;
import com.athuahua.spzx.utils.BaseContext;
import com.athuahua.spzx.utils.MenuHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.athuahua.spzx.entity.SysMenu;
import com.athuahua.spzx.service.SysMenuService;
import com.athuahua.spzx.mapper.SysMenuMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author huahua
 * @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
 * @createDate 2024-07-24 09:54:31
 */
@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
        implements SysMenuService {


    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenuHasChildren> findNodes() {
        //查询所有菜单
        List<SysMenu> sysMenus = sysMenuMapper.selectList(
                new LambdaQueryWrapper<SysMenu>()
                        .orderByAsc(SysMenu::getSortValue)
        );
        //调用工具类方法，把list集合封装为要求的格式
        //带子节点的菜单
        List<SysMenuHasChildren> newSysMenus = new ArrayList<>();

        for (SysMenu sysMenu : sysMenus) {
            //把sysMenus全部属性都复制给newSysMenus ,BeanUtils 不能拷贝List集合
            SysMenuHasChildren sysMenuHasChildren = new SysMenuHasChildren();
            BeanUtils.copyProperties(sysMenu, sysMenuHasChildren);
            newSysMenus.add(sysMenuHasChildren);
        }


        return MenuHelper.buildTree(newSysMenus);


    }

    @Override
    public List<SysMenuVo> findMenusByUserId() {
        //获取当前用户
        Long currentId = BaseContext.getCurrentId();
        //一个用户多个角色 一个角色多个菜单
        List<SysMenuHasChildren> sysMenus = sysMenuMapper.findMenusByUserId(currentId);


        List<SysMenuVo> sysMenuVos = this.buildMenus(MenuHelper.buildTree(sysMenus));
        return sysMenuVos;
    }

    @Override
    public void updateParent(SysMenu sysMenu) {
        //更新所有父节点为半选中
        updateParentIsHalf(sysMenu.getParentId());
        //查找父节点
        SysMenu sysMenu1 = sysMenuMapper.selectOne(new LambdaQueryWrapper<SysMenu>()
                .eq(SysMenu::getId, sysMenu.getParentId()));
        //递归更新父节点
        if(sysMenu1.getParentId()!= 0){
            updateParent(sysMenu1);
        }
    }

    @Override
    public void updateParentIsHalf(Long parentId) {
        log.info("更新父节点是否半选中，parentId={}", parentId);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getMenuId, parentId));
        sysRoleMenus.forEach(
                sysRoleMenu -> {
                    sysRoleMenu.setIsHalf(1);
                    sysRoleMenuMapper.updateById(sysRoleMenu);
                }
        );

    }



    private List<SysMenuVo> buildMenus(List<SysMenuHasChildren> menus){
        List<SysMenuVo> menuVos = new LinkedList<SysMenuVo>();
        for (SysMenuHasChildren menu : menus){
            SysMenuVo menuVo = new SysMenuVo();
            menuVo.setTitle(menu.getTitle());
            menuVo.setName(menu.getComponent());
            List<SysMenuHasChildren> children = menu.getChildren();
            if(!CollectionUtils.isEmpty(children)){
                menuVo.setChildren(buildMenus(children));
            }
            menuVos.add(menuVo);
        }
        return menuVos;
    }
}




